package com.llsydn.demo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.llsydn.hello.R;

/**
 * @author lilinshen
 * @title 用来显示标题的列表fragment
 * @description 请填写相关描述
 * @date 2018/10/12 8:50
 */
public class TitleListFragment extends ListFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    /**
     * 设置adapter
     *
     * @param savedInstanceState
     */
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //设置listview为单选模式
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        //给listview设置adapter
        setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.fragment_item, DataUtils.TITLES));
        //默认选择第一个item
        getListView().setItemChecked(0, true);
        showDetailFragment(0);
    }

    /**
     * 点击监听
     *
     * @param l
     * @param v
     * @param position
     * @param id
     */
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        showDetailFragment(position);
    }

    /**
     * 显示DetailFragment
     *
     * @param position
     */
    public void showDetailFragment(int position) {
        //设置详情DetailFragment（携带数据）
        DetailFragment detailFragment = new DetailFragment();
        Bundle bundle = new Bundle();
        bundle.putString("DETAIL", DataUtils.DETAILS[position]);
        detailFragment.setArguments(bundle);
        //将其替换到id为fl_main_content的容器布局中
        getFragmentManager().beginTransaction().replace(R.id.fl_main_content, detailFragment).commit();
    }

}
